package com.ruyuan.eshop.diplomacy.manager.custom.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import com.ruyuan.eshop.common.enums.DelFlagEnum;
import com.ruyuan.eshop.diplomacy.domain.entity.SkuSellerRelationDO;
import com.ruyuan.eshop.diplomacy.manager.common.cache.RedisSortedSetCache;
import com.ruyuan.eshop.diplomacy.manager.common.db.AbstractRedisSortedSetDatabase;
import com.ruyuan.eshop.diplomacy.mapper.SkuSellerRelationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.stream.Collectors.toList;

/**
 * 商品与卖家关系
 *
 * @author zhonghuashishan
 */
@Slf4j
@Service("skuSellerStringDatabase")
public class SkuSellerSortedSetDatabase extends AbstractRedisSortedSetDatabase {

    @Resource
    private SkuSellerRelationMapper skuSellerRelationMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "SKU_SELLER_RELATION";

    /**
     * 卖家ID
     */
    public static final String SELLER_ID = "sellerId";

    /**
     * 数据源单表
     *
     * @return java.lang.String
     * @author zhonghuashishan
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 数据源全量表
     *
     * @param
     * @return java.util.Set<java.lang.String>
     * @author zhonghuashishan
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(TABLE_NAME);
    }


    /**
     * 获取表数据
     *
     * @param tableFieldsMap    key 数据库字段，value 字段值
     * @param queryType         查询类型，多数据查询时区分
     * @return java.util.Optional<com.ruyuan.eshop.diplomacy.domain.entity.SkuInfoDO>
     * @author zhonghuashishan
     */
    @Override
    public Optional<SkuSellerRelationDO> getTableData(Map tableFieldsMap, String queryType) {
        if (!tableFieldsMap.containsKey(SELLER_ID)) {
            throw new IllegalArgumentException();
        }

        // 卖家基本信息
        String sellerId = (String) tableFieldsMap.get(SELLER_ID);
        QueryWrapper<SkuSellerRelationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SELLER_ID", sellerId);
        SkuSellerRelationDO skuSellerRelationDO = skuSellerRelationMapper.selectOne(queryWrapper);
        return null == skuSellerRelationDO ? Optional.empty() : Optional.of(skuSellerRelationDO);
    }
    /**
     *  根据卖家ID批量获取数据
     * @param idSet         表主键ID集合
     * @param queryType     查询类型，多数据查询时区分
     * @return
     */
    @Override
    public Optional<RedisSortedSetCache> listTableDataByIdSet(Set idSet, String queryType) {
        QueryWrapper<SkuSellerRelationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("sellerId", idSet);
        List<SkuSellerRelationDO> skuSellerRelationDOList = skuSellerRelationMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(skuSellerRelationDOList)) {
            return Optional.empty();
        }
        RedisSortedSetCache<SkuSellerRelationDO> redisStringCache = new RedisSortedSetCache<>();
        List<SkuSellerRelationDO> addList = skuSellerRelationDOList.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.EFFECTIVE.getCode())).collect(toList());
        redisStringCache.setAddList(addList);
        return Optional.of(redisStringCache);
    }

    /**
     * 获取全量数据
     *
     * @param tableName 表名
     * @param idSet     指定主键ID
     * @return java.util.Optional<java.util.Map < java.lang.String, com.ruyuan.eshop.diplomacy.manager.common.source.RedisSortedSetSource>> key 表名，value 主键ID集合
     * @author zhonghuashishan
     */
    @Override
    public Optional<Map<String, RedisSortedSetCache>> listIncrMap(String tableName, Set idSet) {
        return Optional.empty();
    }


}
